package com.jams.goodox.activity.qa.parent;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.DefaultItemAnimator;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jams.goodox.BaseFragment;
import com.jams.goodox.R;
import com.jams.goodox.SePref;
import com.jams.goodox.activity.login.CommSearchActivity;
import com.jams.goodox.activity.qa.entity.Question;
import com.jams.goodox.activity.qa.entity.Teacher;
import com.jams.goodox.adapter.lfadapter.CommonRecycleViewAdapter;
import com.jams.goodox.adapter.lfadapter.LFRecyclerViewHolder;
import com.jams.goodox.net.ProtocolManagers;
import com.jams.goodox.net.services.LoginService;
import com.jams.goodox.utils.ToastUtil;
import com.jams.goodox.widget.CustomPopWindow;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import me.leefeng.lfrecyclerview.LFRecyclerView;
import me.leefeng.lfrecyclerview.OnItemClickListener;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @author HP
 * @date 2018/8/8
 * <p>
 */

public class TeacherListFragment extends BaseFragment {

    private static final String TAG = "TeacherListFragment";
    private View rootView;

//    @Bind(R.id.search_btn)
//    TextView search_btn;


    @Bind(R.id.flyt_fragments)
    FrameLayout flyt_fragments;

    @Bind(R.id.list_layout)
    LinearLayout list_layout;

    private Intent intent;

    @Bind(R.id.unlogin_show)
    LinearLayout unlogin_show;

    @Bind(R.id.left)
    TextView left;
    @Bind(R.id.right)
    TextView right;

    @Bind(R.id.grid_type)
    TextView grid_type;

    BaseFragment leftFragment;
    BaseFragment rightFragment;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (rootView == null) {
            rootView = inflater.inflate(R.layout.fragment_tacherlist, container, false);
            // 初始化页面
            ButterKnife.bind(this, rootView);
        }
        ButterKnife.bind(this, rootView);

        init();
        //requestData();
        return rootView;
    }


    protected void init() {
//        search_btn.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                intent = new Intent(getActivity(),CommSearchActivity.class);
//                startActivity(intent);
//            }
//        });

        grid_type.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                intent = new Intent(getActivity(),ParentAnswerListActivity.class);
                startActivity(intent);
            }
        });

        left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switchLeftFragment();
            }
        });

        right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switchRightFragment();
            }
        });


        switchLeftFragment();
    }


    public void switchLeftFragment() {
        if (leftFragment != null && !leftFragment.isHidden()) {
            return;
        }
        left.setSelected(true);
        right.setSelected(false);

        FragmentManager manager = getChildFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        if (rightFragment != null) {
            transaction.hide(rightFragment);
        }

        if (leftFragment == null) {
            leftFragment = new LeftFragment();
            Bundle bundle = new Bundle();
            bundle.putBoolean("tag", true);
            leftFragment.setArguments(bundle);
            transaction.add(R.id.flyt_fragments, leftFragment);
        } else {
            transaction.show(leftFragment);
        }

        transaction.commit();
    }

    public void switchRightFragment() {
        if (rightFragment != null && !rightFragment.isHidden()) {
            return;
        }
        left.setSelected(false);
        right.setSelected(true);

        FragmentManager manager = getChildFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        if (leftFragment != null) {
            transaction.hide(leftFragment);
        }

        if (rightFragment == null) {
            rightFragment = new RigthFragment();
            Bundle bundle = new Bundle();
            bundle.putBoolean("tag", true);
            rightFragment.setArguments(bundle);
            transaction.add(R.id.flyt_fragments, rightFragment);
        } else {
            transaction.show(rightFragment);
        }

        transaction.commit();
    }


    @Override
    public void onResume() {
        super.onResume();
        if (!SePref.isLogin()){
            //unlogin_show.setVisibility(View.VISIBLE);
            list_layout.setVisibility(View.VISIBLE);
            grid_type.setVisibility(View.GONE);
//            search_btn.setVisibility(View.GONE);
        }else {
            //unlogin_show.setVisibility(View.GONE);
            list_layout.setVisibility(View.VISIBLE);
//            search_btn.setVisibility(View.VISIBLE);
            grid_type.setVisibility(View.VISIBLE);
        }
    }
}
